package com.example.mobifin_busticket.views.adapter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.example.mobifin_busticket.AppData;
import com.example.mobifin_busticket.R;

import com.example.mobifin_busticket.models.Boarding_DropingPointModel;
import com.example.mobifin_busticket.models.SourceModel;

import com.example.mobifin_busticket.views.adapter.FilterBusAdapter.ViewHolder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.Filter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomListAdapterForBoardingPoint extends BaseAdapter {
	class ViewHolder {

		TextView tvBoardingName;
		CheckBox chkbox;
		LinearLayout llFilter;
	}

	private Context mContext;

	private List<Boarding_DropingPointModel> lstboardingpointTemp = null;
	ArrayList<Boarding_DropingPointModel> BoardingList = null;
	DateFormat format;
	private List<Boarding_DropingPointModel> originalData = null;

	private ArrayList<Boarding_DropingPointModel> listvaltemp = null;
	static String selectedValue = null;

	String time;
	LayoutInflater vi;
	private ItemFilter mFilter = new ItemFilter();
	ViewHolder holder = new ViewHolder();
	boolean[] itemChecked;
	Integer selected_position = -1;

	public CustomListAdapterForBoardingPoint(Context ctx,
			ArrayList<Boarding_DropingPointModel> listvalue,
			String SelectedValue) {
		this.mContext = ctx;
		this.BoardingList = listvalue;
		this.originalData = listvalue;
		this.selectedValue = SelectedValue;
		this.lstboardingpointTemp = new ArrayList<Boarding_DropingPointModel>();
		this.lstboardingpointTemp.addAll(listvalue);
		format = new SimpleDateFormat("yyyy-MM-dd hh:mm a");

		itemChecked = new boolean[listvalue.size()];
		vi = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return BoardingList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return BoardingList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		System.out.println("In Get View==>position:" + position);
		final Boarding_DropingPointModel boardingModel = BoardingList
				.get(position);
		View v = convertView;

		if (v == null) {

			holder = new ViewHolder();
			v = vi.inflate(R.layout.row_boardingpoint_sarch, null);
			holder.tvBoardingName = (TextView) v
					.findViewById(R.id.txt_Boardinglist);
			holder.chkbox = (CheckBox) v.findViewById(R.id.chk_Boardingitem);
			holder.llFilter = (LinearLayout) v.findViewById(R.id.llBoarding);

			v.setTag(holder);

		} else {
			holder = (ViewHolder) v.getTag();
		}
		holder.tvBoardingName.setText(BoardingList.get(position)
				.getBordingPointName()
				+ " "
				+ "("
				+ getboardingTimewithFormat(BoardingList.get(position)
						.gettime()) + ")");
		holder.chkbox.setChecked(itemChecked[position]);
		holder.chkbox.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				for (int i = 0; i < itemChecked.length; i++) {
					if (i == position) {
						itemChecked[i] = true;

						selectedValue = BoardingList.get(i)
								.getBordingPointName()
								+ " "
								+ "("
								+ getboardingTimewithFormat(BoardingList.get(i)
										.gettime()) + ")";

						System.out.println("Selected value:" + selectedValue);
						Intent slctBoardingIntent = new Intent();

						slctBoardingIntent.putExtra("SelectBoarding",
								selectedValue);
						((Activity) mContext).setResult(Activity.RESULT_OK,
								slctBoardingIntent);
						((Activity) mContext).finish();
						((Activity) mContext).overridePendingTransition(
								R.anim.hold, R.anim.push_out_to_bottom);
						holder.chkbox.setChecked(true);

					} else {
						itemChecked[i] = false;
					}
				}
				notifyDataSetChanged();

			}

		});

		holder.chkbox.setId(position);
		// arraycheckBoxs.add(holder.chkbox);
		return v;
	}

	public Filter getFilter() {
		// TODO Auto-generated method stub
		return mFilter;
	}

	private String getboardingTimewithFormat(String boardingPointandTime) {
		String boardingTime = "";
		if (boardingPointandTime.isEmpty()) {
			boardingTime = "";
		} else {
			Date boardingDate = null;
			try {
				boardingDate = format.parse("" + boardingPointandTime);
				DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
				boardingTime = dateFormat.format(boardingDate).toString();
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return boardingTime;
	}

	private class ItemFilter extends Filter {

		@Override
		protected FilterResults performFiltering(CharSequence constraint) {

			String filterString = constraint.toString().toLowerCase();
			FilterResults results = new FilterResults();
			final List<Boarding_DropingPointModel> list = originalData;

			int count = list.size();
			final ArrayList<Boarding_DropingPointModel> boardinglist = new ArrayList<Boarding_DropingPointModel>(
					count);
			Boarding_DropingPointModel filterableString;

			for (int i = 0; i < count; i++) {
				filterableString = list.get(i);
				if (filterableString.getBordingPointName().toLowerCase()
						.contains(filterString)) {
					boardinglist.add(filterableString);
				}
			}
			results.values = boardinglist;
			results.count = boardinglist.size();

			return results;
		}

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			BoardingList = (ArrayList<Boarding_DropingPointModel>) results.values;
			notifyDataSetChanged();
		}
	}

	public static String getSelectedPoint() {
		return selectedValue;
	}

	public static void setSelectedPoint(String selectedPoint) {
		CustomListAdapterForBoardingPoint.selectedValue = selectedValue;
	}

}
